<template>
  <div class="financial-container">
    <v-layout wrap justify-center align-center>
      <v-flex md12 d-flex>
        <v-flex md2 px-2>
          <v-select :items="projectTypes" v-model="queryForm.status" label="项目类型"></v-select>
        </v-flex>

        <v-flex md2 px-2>
          <v-menu offset-x full-width>
            <v-text-field
              color="primary"
              slot="activator"
              v-model="queryForm.beginTime"
              placeholder="开始时间"
              disabled
            ></v-text-field>
            <v-date-picker
              landscape
              min="2019-07-01"
              :first-day-of-week="0"
              locale="zh-cn"
              v-model="queryForm.beginTime"
              color="secondary"
            ></v-date-picker>
          </v-menu>
        </v-flex>
        <v-flex md2 px-2>
          <v-menu offset-x full-width>
            <v-text-field
              color="primary"
              slot="activator"
              v-model="queryForm.endTime"
              placeholder="结束时间"
              disabled
            ></v-text-field>
            <v-date-picker
              landscape
              min="2019-07-01"
              :first-day-of-week="0"
              locale="zh-cn"
              v-model="queryForm.endTime"
              color="secondary"
            ></v-date-picker>
          </v-menu>
        </v-flex>
        <v-flex md2 px-2>
          <v-btn round class="subTitle" color="primary" @click="onQuery">
            <span>查询</span>
            <v-icon>search</v-icon>
          </v-btn>
        </v-flex>
      </v-flex>

      <!-- table -->
      <v-flex md12 class="elevation-1">
        <v-data-table :headers="headers" :loading="loading" :items="tableData" hide-actions>
          <template v-slot:items="props">
            <td class="text-xs-center">{{ props.item.incomeId }}</td>
            <td class="text-xs-center">{{ props.item.nickName }}</td>
            <td class="text-xs-center">{{ props.item.uid }}</td>
            <td class="text-xs-center">{{ props.item.incomeType | IncomeType }}</td>
            <td class="text-xs-center">{{ props.item.project }}</td>
            <td class="text-xs-center">{{ props.item.change }}</td>
            <td class="text-xs-center">{{ props.item.balance }}</td>
            <td class="text-xs-center">{{ getTime(props.item.createTime) }}</td>
            <td class="text-xs-center">{{ props.item.desc }}</td>
            <td class="text-xs-center">{{ props.item.remark }}</td>

          </template>
          <template v-slot:no-data>
            <v-alert :value="true" outline color="warning" icon="info">暂无数据</v-alert>
          </template>
        </v-data-table>

        <v-layout class="pagination-wrapper">
          <v-flex md10 d-flex justify-start align-center>
            <v-flex d-flex md2 justify-start align-center>
              <v-flex md4 class="title-color">显示</v-flex>
              <v-flex md4>
                <v-select @change="onPageOptionChange" :items="pageOptions" v-model="pagination.limit"></v-select>
              </v-flex>
              <v-flex md4 class="title-color">条/页</v-flex>
            </v-flex>
            <v-flex md2 class="title-color">共 {{ pagination.pageCount }} 条数据</v-flex>
            <v-flex md2></v-flex>
            <v-flex md4>
              <v-pagination
                @input="onPageChange"
                :length="pagination.pageCount"
                circle
                total-visible="5"
                v-model="pagination.page"
              />
            </v-flex>
            <v-flex md2></v-flex>
          </v-flex>
        </v-layout>
      </v-flex>
    </v-layout>

    <v-dialog v-model="dialog" max-width="800">
      <v-card>
        <v-card-title class="headline grey lighten-2" primary-title>{{ dialogTitle }}</v-card-title>
        <v-card-text>
          <v-layout justify-center align-center v-if="dialogType === 1">
            <v-flex md12>
              
            </v-flex>
          </v-layout>
        </v-card-text>
        <v-card-actions>
          <v-spacer></v-spacer>
          <v-btn color="blue darken-1" flat @click="onDialogConfirm">确认</v-btn>
          <v-btn color="grey darken-1" flat @click="dialog = false">取消</v-btn>
        </v-card-actions>
      </v-card>
    </v-dialog>

    <v-dialog v-model="modal" persistent max-width="290">
      <v-card>
        <v-card-title class="title">{{ modalTitle }}</v-card-title>
        <v-card-text class="title">{{ modalContent }}</v-card-text>
        <v-card-actions>
          <v-spacer></v-spacer>
          <v-btn color="blue darken-1" flat @click="onModalConfirm">确定</v-btn>
          <v-btn color="grey darken-1" flat @click="modal = false">取消</v-btn>
        </v-card-actions>
      </v-card>
    </v-dialog>
  </div>
</template>


<script lang="ts">
// 使用类装饰器
import { Component, Vue } from "vue-property-decorator";
import { Todo } from "@/api";
import { TaskStatusKV, PageOptions, ProjectTypeKV } from "@/utils/constants";
import { KV, Pagination } from  "@/utils/type";
import { forEach } from "lodash";
import { getTime } from '@/utils/commonUtils';
import { ReqProfitIncome } from '@/api/bean';
declare const $Vue: Vue;

@Component
export default class OrderList extends Vue {
  private projectTypes: Array<KV> = ProjectTypeKV;
  private pageOptions: any = [];

  private queryForm: any = {
    projectType: "",
    nickName: "",
    status: 0
  };

  private headers: Array<any> = [
    { text: "ID", align: "center", sortable: false, value: "id" },
    { text: "用户信息", align: "center", sortable: false, value: "taskId" },
    { text: "UID", align: "center", sortable: false, value: "platform" },
    { text: "类型", align: "center", sortable: false, value: "type" },
    { text: "项目", align: "center", sortable: false, value: "seller" },
    { text: "资金变化", align: "center", sortable: false, value: "myshop" },
    { text: "余额", align: "center", sortable: false, },
    { text: "创建时间", align: "center", sortable: false, value: "createTime" },
    { text: "说明", align: "center", sortable: false, value: "updateTime" },
    { text: "操作备注", align: "center", sortable: false, value: "action" }
  ];

  private tableData: any = [
    {
      username: "合适的"
    }
  ];

  private loading: boolean = false;
  private dialog: boolean = false;
  private dialogTitle: string = "";
  private dialogType: number = 1;
  private dialogItem: any = {};

  private modal: boolean = false;
  private modalType: number = 1;
  private modalTitle: string = "注意!";
  private modalItem: any = {};
  private modalContent: string = "确认取消订单吗?";
  private isQuery: boolean = false;

  private pagination: Pagination = {
    pageCount: 20,
    page: 1,
    limit: 10,
    offset: 0,
  };

  protected mounted() {
    this.getFinancialDetailList();
    this.initialize();
  }

  private getTime(date: string) {
    return getTime(date);
  }


  private initialize() {
    // this.topupStatus = TopupStatusKV.slice();
    this.pageOptions = PageOptions.slice();
  }

  private onQuery(from?: string) {
    let params: any = {};

    let valid = 0;
    forEach(this.queryForm, (item, key) => {
      item ? (params[key] = item) : valid++;
    });
    if (valid === 3) {
      return $Vue.$emit("snackbar", {
        message: "请至少填写一个查询条件"
      });
    }

    if (from === undefined) {
      this.pagination.offset = 0;
      this.pagination.page = 1;
    }

    if (this.queryForm.beginTime && this.queryForm.endTime) {
      if (
        new Date(this.queryForm.beginTime).getTime() >
        new Date(this.queryForm.endTime).getTime()
      ) {
        return $Vue.$emit("snackbar", {
          text: "起始日期不能早于结束日期"
        });
      }
      params.endTime = new Date(this.queryForm.endTime + " 23:59:59").getTime();
      params.beginTime = new Date(
        this.queryForm.beginTime + " 00:00:00"
      ).getTime();
    } else if (this.queryForm.beginTime || this.queryForm.endTime) {
      return $Vue.$emit("snackbar", {
        text: "请选择完整起止日期"
      });
    }

    params.limit = this.pagination.limit;
    params.offset = this.pagination.offset;
    this.getFinancialDetailList(params);
    this.isQuery = true;
  }


  private onPageChange(page: number) {
    this.pagination.offset = (page - 1) * this.pagination.limit;
    if (!this.isQuery) {
      this.getFinancialDetailList();
    } else {
      this.onQuery("wyvonj");
    }
  }

  private onPageOptionChange(value: any) {
    this.pagination.page = 1;
    this.pagination.offset = 0;
  }


  private onModalConfirm() {
    if (this.modalType === 1) {

    }
  }

  private onDialogConfirm() {
    if (this.modalType === 1) {

    }
  }


  /**
   * 获取任务列表
   */
  private async getFinancialDetailList(queryForm?: any) {
    let params: ReqProfitIncome = {
      offset: this.pagination.offset,
      limit: this.pagination.limit
    };
    if (queryForm) {
      params = queryForm;
    }
    const { data } = await Todo.getProfitIncomeList.r(params);
    console.log("getFinancialDetailList", data);
    if (data.result === 0) {
      let $data = data.data;
      this.tableData = $data.dataList;

      this.pagination.pageCount = $data.totalCount
        ? Math.ceil($data.totalCount / this.pagination.limit)
        : 1;
    } else {
      $Vue.$emit("snackbar", {
        message: data.message
      });
    }
  }

}
</script>

<style lang="less" scoped>
.financial-container {
  width: 100%;
  height: 100%;
}
.pagination-wrapper {
  background: white;
  border-top: 1px solid #d4d4d4;
}
</style>


